package com.thssh.ui.lsn02.homework.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.View;
import android.view.ViewGroup;


/**
 * 简单实现瀑布流效果
 *
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/09/07
 */

public class TableLayout extends ViewGroup{

    private int mColumns = 3;
    private SparseIntArray mRowMaxHeights;
    private SparseIntArray mColumnMaxWidths;
    private SparseArray<SparseArray<View>> mRowViews;

    public TableLayout(Context context) {
        super(context);
    }

    public TableLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TableLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private int measureCount = 0;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureCount ++;
        mRowMaxHeights = new SparseIntArray();
        mRowViews = new SparseArray<>();
        mColumnMaxWidths = new SparseIntArray();
        if(mColumns < 1){ return; }
        int measuredWidth = 0;
        int measuredHeight = 0;

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int suggestWidthSize = MeasureSpec.getSize(widthMeasureSpec);
        int suggestHeightSize = MeasureSpec.getSize(heightMeasureSpec);

        measureChildren(widthMeasureSpec, heightMeasureSpec);

        int childCount = getChildCount();
//        Log.d("WaterfallLayout", "childCount: " + childCount);
        /** 子控件行数 */
        int rowCount = childCount / mColumns;
        if(childCount % mColumns != 0){
            rowCount ++;
        }

//        int maxRowWidth = 0;
        int maxRowHeight = 0;

        int averageRowWidth = suggestWidthSize / mColumns;
        for(int i = 0; i < rowCount; i++){
            // 每行子控件最大高度位当前行子控件最大高度
            // 每行子控件最大宽度为当前行子控件最大宽度，但如果此宽度大于平均列宽度则取列宽度
            SparseArray<View> views = new SparseArray<>();
            for(int j = 0; j < mColumns; j ++){
                int index = i * mColumns + j;
//                Log.d("WaterfallLayout", "onMeasure: " + index);
                if(index >= childCount){ break; }
                View child = getChildAt(index);
                views.put(j, child);
                int childHeight = child.getMeasuredHeight();
                int childWidth = child.getMeasuredWidth();
                maxRowHeight = Math.max(maxRowHeight, childHeight);
//                maxRowWidth = Math.max(maxRowWidth, childWidth);
//                if(childWidth > averageRowWidth){
//                    maxRowWidth = averageRowWidth;
//                }
                int columnMaxWidth = mColumnMaxWidths.valueAt(j);
//                Log.d("WaterfallLayout", "childWidth: " + childWidth);
                columnMaxWidth = Math.max(columnMaxWidth, childWidth);
                if(childWidth > averageRowWidth){
                    columnMaxWidth = averageRowWidth;
                }
                // 可以加个判断。如果要让子view充满整个空间
                columnMaxWidth = averageRowWidth;
                mColumnMaxWidths.put(j, columnMaxWidth);
            }
            mRowViews.put(i, views);
            mRowMaxHeights.put(i, maxRowHeight);
            measuredHeight += maxRowHeight;
//            Log.d("WaterfallLayout", "row: " + i + " maxRowHeight: " + maxRowHeight);
//            Log.d("WaterfallLayout", "row: " + i + " maxRowWidth: " + maxRowWidth);
        }
        measuredWidth = suggestWidthSize;

        setMeasuredDimension(measuredWidth, measuredHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
//        Log.d("WaterfallLayout", "measureCount: " + measureCount);

        int lastRight = 0;
        int lastBottom = 0;
        for(int i = 0; i < mRowViews.size(); i++){
            int curRowHeight = mRowMaxHeights.valueAt(i);
//            Log.d("WaterfallLayout", "curRowHeight: " + curRowHeight);
            SparseArray<View> views = mRowViews.valueAt(i);
            for(int j = 0; j < views.size(); j++){
                int left = 0;
                int top = 0;
                int right = 0;
                int bottom = 0;
                int columnMaxWidth = mColumnMaxWidths.valueAt(j);
//                Log.d("WaterfallLayout", "columnMaxWidth: " + columnMaxWidth);
                View child = views.valueAt(j);
                left = lastRight;
                top = lastBottom;
                right = left + columnMaxWidth;
                bottom = top + child.getMeasuredHeight();
                LayoutParams layoutParams = child.getLayoutParams();
                if(layoutParams instanceof MarginLayoutParams){
                    MarginLayoutParams mlp = (MarginLayoutParams) layoutParams;
                    left += mlp.leftMargin;
                    right += mlp.rightMargin;
                }
                child.layout(left, top, right, bottom);
//                Log.d("WaterfallLayout", child.hashCode() + " i: " + i + " j: " + j +" <> left: " + left + " top: " + top + " right: " + right + " bottom: " + bottom);
                lastRight = right;
            }
            lastRight = 0;
            lastBottom += curRowHeight;
        }
    }
}
